{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85ec013d",
   "metadata": {},
   "source": [
    "----\n",
    "- 日期：2021/11/1\n",
    "- 内容：python\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63701306",
   "metadata": {},
   "source": [
    "# 本周学习内容：\n",
    "> 1. 字典的方法\n",
    "> 2. 实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5d1ac6ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"jenny\",\n",
    "    1800002:\"marry\",\n",
    "    1800003:\"gogo\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3a2500e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'jenny', 1800002: 'marry', 1800003: 'gogo'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(user_list,type(user_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e228a586",
   "metadata": {},
   "source": [
    "user_list[0]  ##无法查看，证明字典是无序的，没办法取索引值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ff984f3",
   "metadata": {},
   "source": [
    "# 1.字典的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96055a63",
   "metadata": {},
   "source": [
    "## 1.1 数据结构的常见方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa754e1c",
   "metadata": {},
   "source": [
    "- len() 用于查看长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5fb3a5a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(user_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf023133",
   "metadata": {},
   "source": [
    "- in用于查询成员是否存在: 查询的是key是否存在，而不是查询value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f2d0fada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1800001 in user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3c646426",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jenny'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cbef8da",
   "metadata": {},
   "source": [
    "## 1.2属于字典的一些方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cab5ad4",
   "metadata": {},
   "source": [
    "> 1. 增删改查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a527d70",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"jenny\",\n",
    "    1800002:\"marry\",\n",
    "    1800003:\"gogo\",\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f754d4b7",
   "metadata": {},
   "source": [
    "- 字典的增加\n",
    "> 1. 先查询要增加的key是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e037d72",
   "metadata": {},
   "outputs": [],
   "source": [
    "if 1800004 in user_list:\n",
    "    print(\"该用户已存在，请更换key\")\n",
    "else:\n",
    "    user_list[1800004] = \"lala\"\n",
    "    print(\"增加成功哈哈\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77bdc1c3",
   "metadata": {},
   "source": [
    "- 字典的修改\n",
    "> 1. 先查询要修改的key是否存在\n",
    "> 2. 如果不存在key，可能考虑是否新增用户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3eac1c34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信息更新成功\n"
     ]
    }
   ],
   "source": [
    "if 1800001 in user_list:\n",
    "    user_list[1800001]=\"jan\"\n",
    "    print(\"信息更新成功\")\n",
    "else:\n",
    "    print(\"该key找不到\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e953de05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'jan', 1800002: 'marry', 1800003: 'gogo', 1800004: '欣欣'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf801b53",
   "metadata": {},
   "source": [
    "- 字典的查询:get() 可以查询到该key的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e274677a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jan'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.get(1800001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "eaf329f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(user_list.get(1800006))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f60ca8c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'jan'), (1800002, 'marry'), (1800003, 'gogo'), (1800004, '欣欣')])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3d3047b",
   "metadata": {},
   "source": [
    "- 字典的循环遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9769c816",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回顾：关于序列类型的数据 循环\n",
    "strings = \"hello world\"\n",
    "lists = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6be8c95d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e6f221e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'jan'), (1800002, 'marry'), (1800003, 'gogo'), (1800004, '欣欣')])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ca1fdf64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800001 jan\n",
      "1800002 marry\n",
      "1800003 gogo\n",
      "1800004 欣欣\n"
     ]
    }
   ],
   "source": [
    "# 字典的循环\n",
    "for k,v in user_list.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9b579ee3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys([1800001, 1800002, 1800003, 1800004])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dict.keys() :取出全部的key\n",
    "user_list.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4469f37b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['jan', 'marry', 'gogo', '欣欣'])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ab6c5200",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800004, '欣欣')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# popitem(): 随机删除1个\n",
    "user_list.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e765b0d",
   "metadata": {},
   "source": [
    "# 字典的两个高级用法\n",
    "- fromkeys\n",
    "- setdefault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "12c95cf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 复位，重启，重置\n",
    "help(user_list.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7ac0f240",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jan': 10, 'marry': 10, 'gogo': 10}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置系统人物的属性，所有人族hp10\n",
    "user_list.values()\n",
    "re_game = {}\n",
    "re_game.fromkeys(user_list.values(),10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "55fa8656",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# \n",
    "help(user_list.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05ef6343",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8f83be5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "723dab39",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e799cd6c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8ada9b9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a57ca72c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e265764e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bd9031d7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a579f5bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'jenny', 1800002: 'marry', 1800003: 'gogo', 1800004: '欣欣'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4be6fc1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad54b680",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21a548a7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "537e89a7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1baada65",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4594f212",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4de9345",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "648d3944",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e3878ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc0a74d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64df7e47",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3057fc2c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68e16e97",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73e3cba5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d487ed18",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efc7ee2d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7b81816",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05ab2ee5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "047d0bb9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42cebede",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d14ca3d3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dca60ae9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf83216e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
